<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Package;

/**
 * PackageSearch represents the model behind the search form about `common\models\Package`.
 */
class PackageSearch extends Package
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'category', 'type', 'province', 'city', 'principal_range', 'state', 'origin', 'owner', 'teamwork', 'lock_user'], 'integer'],
            [['code', 'title', 'access', 'access_users', 'creditor', 'principal', 'interest', 'hushu', 'detail', 'industry', 'created_at', 'updated_at', 'lock_timestamp'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Package::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'category' => $this->category,
            'type' => $this->type,
            'province' => $this->province,
            'city' => $this->city,
            'principal_range' => $this->principal_range,
            'state' => $this->state,
            'origin' => $this->origin,
            'owner' => $this->owner,
            'teamwork' => $this->teamwork,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'lock_user' => $this->lock_user,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'access', $this->access])
            ->andFilterWhere(['like', 'access_users', $this->access_users])
            ->andFilterWhere(['like', 'creditor', $this->creditor])
            ->andFilterWhere(['like', 'principal', $this->principal])
            ->andFilterWhere(['like', 'interest', $this->interest])
            ->andFilterWhere(['like', 'hushu', $this->hushu])
            ->andFilterWhere(['like', 'detail', $this->detail])
            ->andFilterWhere(['like', 'industry', $this->industry])
            //->andFilterWhere(['like', 'attachment', $this->attachment])
            ->andFilterWhere(['like', 'lock_timestamp', $this->lock_timestamp]);

        return $dataProvider;
    }
}
